package pageObject;

import org.openqa.selenium.*;
import utils.ReportWriter;

public class PurchasePage extends BasePage {

    protected PurchasePage(WebDriver driver) {
        super(driver);
    }

    protected final static By nameId = By.id("reciever_name");
    protected final static By cityKievChooseXpath = By.xpath("//a[@locality_id='1']");
    protected final static By phoneId = By.id("reciever_phone");
    protected final static By continueButtonXpath = By.xpath("//*[@id=\"step_contacts\"]/div/div[1]/div[3]/div/span/button");
    protected final static By orderPlacementClickable = By.xpath("//*[@id=\"total_block\"]/div[2]/span[@class = 'btn-link check-step-btn-link check-submit']");
    protected final static By userNameInput = By.name("order[1][delivery][recipient_title]");
    protected final static By permanentUserLinkXpath = By.xpath("//li[2]/a");
    protected final static By userEmailName = By.name("user[login]");
    protected final static By userPasswordName = By.name("user[password]");
    protected final static By signInButtonId = By.id("signin");
    protected final static By incorrectUserInputXpath = By.xpath("//*[@id=\"step_contacts\"]/div/div[2]/div/div[3]/div[1]/div[3]/h5");
    protected final static By editCartLinkName = By.name("edit");
    protected final static By quantityName = By.name("quantity");
    protected final static By submitQuantityXpath = By.xpath("//*[@id=\"cart-popup\"]/div/div/div[1]/div[2]/div/div[4]/div/a/img");

    public void fillUserInformation(String userName, String phoneNumber) {
        driver.findElement(nameId).clear();
        driver.findElement(nameId).sendKeys(userName);
        driver.findElement(cityKievChooseXpath).click();
        driver.findElement(phoneId).clear();
        driver.findElement(phoneId).sendKeys(phoneNumber);
        driver.findElement(continueButtonXpath).click();
    }

    public boolean canBeBought(String userName) {
        driver.findElement(userNameInput).clear();
        driver.findElement(userNameInput).sendKeys(userName);

        boolean success = false;
        try {
            if (driver.findElement(orderPlacementClickable).isEnabled()) {
                ReportWriter.info("Product can be bought by user "+userName+" !");
                success = true;
            }
        } catch (NoSuchElementException ex) {
            ReportWriter.info("Product can NOT be bought by user "+userName+" !");
            success = false;
        } finally {
            return success;
        }
    }

    public boolean checkUserInformationWhilePurchase(String email, String password) {
        driver.findElement(permanentUserLinkXpath).click();
        driver.findElement(userEmailName).clear();
        driver.findElement(userEmailName).sendKeys(email);
        driver.findElement(userPasswordName).clear();
        driver.findElement(userPasswordName).sendKeys(password);
        driver.findElement(signInButtonId).click();
        boolean success = true;
           try {
                Thread.sleep(3000);
                if(driver.findElement(incorrectUserInputXpath).isDisplayed()) {
                    ReportWriter.info("User " + email + " is NOT logged in!");
                    success = true;
                }
            } catch (NoSuchElementException ex) {
                ReportWriter.info("User " + email + " is logged in!");
                success = false;
            } catch (Exception ex) {
                ReportWriter.info("Caught exception while sleep");
            } finally {
                return success;
            }
        }

    public void changeQuantity() {
        driver.findElement(editCartLinkName).click();
        driver.findElement(quantityName).clear();
        driver.findElement(quantityName).sendKeys("0");
        driver.findElement(submitQuantityXpath).click();
        try {
            Thread.sleep(2000);
        } catch (Exception e) {
            ReportWriter.info("Caught exception while sleep");
        }
    }

}
